package com.paw.ruoyi.admin.controller.sms;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.paw.ruoyi.admin.common.annotion.OperatorLog;
import com.paw.ruoyi.admin.common.constant.BusinessType;
import com.paw.ruoyi.admin.common.constant.CommonConstant;
import com.paw.ruoyi.admin.common.domain.Result;
import com.paw.ruoyi.admin.common.domain.PageData;
import com.paw.ruoyi.admin.common.annotion.RequiresPermissions;
import com.paw.ruoyi.admin.common.utils.HuExcelUtil;
import com.paw.ruoyi.admin.common.annotion.SkipLogParam;
import com.paw.ruoyi.admin.common.annotion.SkipResponseLog;
import com.paw.ruoyi.admin.common.domain.PageDomain;
import com.paw.ruoyi.admin.core.util.TableSupport;
import com.paw.ruoyi.admin.domain.SmsTemplate;
import com.paw.ruoyi.admin.service.ISmsTemplateService;


/**
 * 短信模板Controller
 *
 * @author admin
 * @date 2022-03-13
 */
@RestController
@RequestMapping("/api/sms/smsTemplate")
public class SmsTemplateController {

    @Autowired
    private ISmsTemplateService smsTemplateService;

    /**
     * 查询短信模板列表
     */
    @RequiresPermissions("sms:smsTemplate:list")
    @GetMapping("/list")
    public Result<PageData<SmsTemplate>> list(SmsTemplate smsTemplate) {

      PageDomain pageDomain = TableSupport.buildPageRequest();
      LambdaQueryWrapper<SmsTemplate> queryWrapper = new LambdaQueryWrapper<>(smsTemplate);

      // like query
      if(StrUtil.isNotEmpty(smsTemplate.getSysServiceName())){
        queryWrapper.like(SmsTemplate::getSysServiceName,smsTemplate.getSysServiceName());
        smsTemplate.setSysServiceName(null);
      }
      if(StrUtil.isNotEmpty(smsTemplate.getTemplateName())){
        queryWrapper.like(SmsTemplate::getTemplateName,smsTemplate.getTemplateName());
        smsTemplate.setTemplateName(null);
      }

      // 排序
      Map<String, Object> paramsMap = smsTemplate.getParams();
      if(ObjectUtil.isNotEmpty(paramsMap) &&
          ObjectUtil.isNotEmpty(paramsMap.get(CommonConstant.TABLE_SORT)) &&
          ObjectUtil.isNotEmpty(paramsMap.get(CommonConstant.TABLE_ORDER))){
        String tableSort = (String) paramsMap.get(CommonConstant.TABLE_SORT);
        String tableOrder = (String) paramsMap.get(CommonConstant.TABLE_ORDER);
        if(CommonConstant.CREATE_TIME.equals(tableSort)){
          queryWrapper.orderBy(StrUtil.isNotEmpty(tableSort),CommonConstant.SORT_ASCEND.equals(tableOrder),SmsTemplate::getCreateTime);
        }
      }
      // queryWrapper.orderBy("createTime".equalsIgnoreCase(pageDomain.getOrderBy()),Boolean.parseBoolean(pageDomain.getIsAsc()),SysDictType::getCreateTime);
      Page<SmsTemplate> page = smsTemplateService.page(new Page<>(pageDomain.getPageNum(), pageDomain.getPageSize()), queryWrapper);
      return Result.page(page.getRecords(),page.getTotal());
    }

    /**
     * 导出短信模板列表
     */
    @SkipLogParam
    @SkipResponseLog
    @RequiresPermissions("sms:smsTemplate:export")
    @OperatorLog(title = "短信模板", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SmsTemplate smsTemplate) {
      List<SmsTemplate> list = smsTemplateService.list(Wrappers.lambdaQuery(smsTemplate));
      HuExcelUtil.write(response, list, SmsTemplate.class);
    }

    /**
     * 获取短信模板详细信息
     */
    @RequiresPermissions("sms:smsTemplate:query")
    @GetMapping(value = "/detail/{id}")
    public Result<SmsTemplate> getInfo(@PathVariable("id") Long id) {
      return Result.result(smsTemplateService.getById(id));
    }

    /**
     * 新增短信模板
     */
    @RequiresPermissions("sms:smsTemplate:add")
    @OperatorLog(title = "短信模板", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SmsTemplate smsTemplate) {
      boolean success =  smsTemplateService.save(smsTemplate);

      return Result.result(success);
    }

    /**
     * 修改短信模板
     */
    @RequiresPermissions("sms:smsTemplate:edit")
    @OperatorLog(title = "短信模板", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public Result<Boolean> edit(@RequestBody SmsTemplate smsTemplate) {
      boolean success =  smsTemplateService.updateById(smsTemplate);
      return Result.result(success);
    }

    /**
     * 删除短信模板
     */
    @RequiresPermissions("sms:smsTemplate:remove")
    @OperatorLog(title = "短信模板", businessType = BusinessType.DELETE)
	@PostMapping("/batchRemove/{ids}")
    public Result<Boolean> remove(@PathVariable List<Long> ids) {
      boolean success =  smsTemplateService.removeBatchByIds(ids);
      return Result.result(success);
    }
}
